package com.lzlearn.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TranscationConfig {
    @Before("execution(* com.lzlearn.service.*.*(..))")
    public void start(){
        System.out.println("前置方法");
    }
    @AfterReturning("execution(* com.lzlearn.service.*.*(..))")
    public void after(){
        System.out.println("后置方法");
    }
    @After("execution(* com.lzlearn.service.*.*(..))")
    public void finalMethod(){
        System.out.println("最终方法");
    }
    @AfterThrowing("execution(* com.lzlearn.service.*.*(..))")
    public void errMethod(){
        System.out.println("异常方法");
    }

    @Around("execution(* com.lzlearn.service.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        Object result = null;
        try {
            System.out.println("开启事务");
            result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
            System.out.println("提交事务");
        } catch (Throwable throwable) {
            System.out.println("回滚事务");
            throwable.printStackTrace();
        } finally {
            System.out.println("关闭事务");
        }
        return result;
    }
}
